var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        newsId:'',
        _isEdit:0,
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.newsId = Pub.getSession('newsId');
        this.params._isEdit = Pub.getSession('_isEdit');

    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            newsId:parseInt(_this.params.newsId),
        };
        Api.newslistdetails(paramsObj, function(data){
            console.log(data);
            var data=data.data;
            $('#title').val(data.title);
            $('#industryInvolved').val(data.industryInvolved);
            $('#cost').val(data.cost);
            $('#guideLanguage').val(data.guideLanguage);
            $('#prepareTime').val(data.prepareTime);
            $('#classifySelect').val(data.classId);
            $('#reject').val(data.cause);
            var ue = UE.getEditor('container');
            ue.ready(function() {//编辑器初始化完成再赋值
              ue.setContent(data.content);  //赋值给UEditor
            });

            if (data.previewImg!='') {
                $('#image').html('<img src="'+data.previewImg+'" style="height:100px;width:100px;" alt="">')
            };
            $('#name').val(data.linkman);
            $('#mobile').val(data.mobile);
            if (data.isRecommend==0) {
                honeySwitch.showOff("#isRecommend")
            };
        });
    },
    isState:function(){
        var _this = this;
        if (_this.params._isEdit==0) {
                $('#pro-title').text('添加');
            }
        $('.btDiv').on('click', 'div', function(event) {
            event.preventDefault();
            if ($("#isRecommend").hasClass('switch-on')) {
                var isRecommend=1;
            }else{
                var isRecommend=0;
            };
            var content = ue.getContent();
            var paramsObj = {
                passport: _this.params.passport,
                newsId:parseInt(_this.params.newsId),
                title:$('#title').val(),
                classId:$('#classifySelect').val(),
                className:$('#classifySelect').find("option:selected").text(),
                previewImg:$('#image img').attr('src'),
                isRecommend:isRecommend,
                content:content
            };
            var Obj = {
                passport: _this.params.passport,
                title:$('#title').val(),
                classId:$('#classifySelect').val(),
                className:$('#classifySelect').find("option:selected").text(),
                previewImg:$('#image img').attr('src'),
                isRecommend:isRecommend,
                content:content
            };
            // console.log(paramsObj);
            if (_this.params._isEdit==1) {
                Api.newslistmodify(paramsObj, function(data){
                    console.log(data);
                    layui.use('layer', function(){
                        layer.msg('提交成功', {icon: 6});
                        setTimeout(function(){
                            window.location.href = Pub.getHtmlUrl('./the-news-list.html');
                        },1500)
                    });
                });
            }else{
                Api.newslistadd(Obj, function(data){
                    console.log(data);
                    layui.use('layer', function(){
                        layer.msg('提交成功', {icon: 6});
                        setTimeout(function(){
                            window.location.href = Pub.getHtmlUrl('./the-news-list.html');
                        },1500)
                    });
                });
            };

        });
    },
    uploadImg: function(){
        var _this = this;
        Api.upload('#up_form3','#file_3', 'headimgurl',_this.params.passport, function(response){
            console.log(response);
            layui.use('layer', function(){
              layer.msg('上传成功');
            });
            $('#image').html('<img src="'+response.data.imgUrl+'" style="height:100px;width:100px;" >');

        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#money').keyup(function(){
            var c=$(this);
            if(/[^\d]/.test(c.val())){//替换非数字字符
              var temp_amount=c.val().replace(/[^\d]/g,'');
              $(this).val(temp_amount);
            }
        })
    },
    programclasslistget:function(){
        var _this=this;
        var paramsObj = {
            passport: _this.params.passport,
            type:6
        };
        Api.newpulldown(paramsObj, function(data){
            console.log(data);
            _this.programclasslist(data.data)
        })
    },
    programclasslist:function(data){
        var _this=this;
        if (data && data.length > 0) {
            var html = '<option value="" selected>请选择分类</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].classId+'">'+data[i].className+'</option>';
            }
            $('#classifySelect').html(html);
            _this.programclasslistEvent();

        }
    },
    programclasslistEvent:function(){
        var _this=this;
        $('#classifySelect').on('change', function(event) {
            event.preventDefault();
            _this.params.classId=$(this).val();
            _this.params.className=$(this).find("option:selected").text();
        });
    },
    initEvent: function(){
        var _this = this;
        if (_this.params._isEdit==1) {
            setTimeout(function(){
                _this.getData();
            },500)
        };
        _this.otherEvent();
        _this.isState();
        _this.programclasslistget();
         _this.uploadImg();


    }
};
var page = new Page();